// myself/user/info.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import notice from '../../utils/notice.js';
import formatDate from '../../utils/format_date';
import utils from '../../utils/utils';

const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        dateVisible: false,
        // 指定选择区间起始值
        start: '1970-01-01 00:00:00'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let userInfo = app.globalData.userInfo;
        if (!userInfo.birth) {
            userInfo.birth = '2000-01-01';
        } else {
            userInfo.birth = formatDate.formatTime(userInfo.birth, 'yyyy-MM-dd');
        }
        this.setData({
            userInfo
        })
    },

    // getWxInfo() {
    //     wx.getUserProfile({
    //         desc: '用于完善用户资料',
    //         success: (res) => {
    //             this.setData({
    //                 [`userInfo.avatar`]: res.userInfo.avatarUrl,
    //                 [`userInfo.name`]: res.userInfo.nickName,
    //                 [`userInfo.sex`]: res.userInfo.gender
    //             })
    //         }
    //     })
    // },
    chooseAvatar(e) {
        network.uploadFile(e.detail.avatarUrl, 'file', {}).then(img => {
            this.setData({
                [`userInfo.avatar`]: img
            })
        })
    },

    inputPhone(e) {
        this.setData({
            [`userInfo.mobile`]: e.detail.value
        })
    },

    getRealtimePhoneNumber(e) {
        network.getJson(api.getUserPhoneNumber, {
            userId: app.globalData.userInfo.id,
            code: e.detail.code
        }).then(res => {
            console.log(res)
            this.setData({
                [`userInfo.mobile`]: res
            })
        })
    },

    showPicker(e) {
        this.setData({
            dateVisible: true,
        });
    },
    handleChange(e) {
        this.setData({
            [`userInfo.sex`]: e.detail.value
        })
    },
    hidePicker() {
        this.setData({
            dateVisible: false
        });
    },
    onConfirm(e) {
        this.setData({
            [`userInfo.birth`]: e.detail.value
        });

        this.hidePicker();
    },

    onSubmit(e) {
        console.log(e)
        this.data.userInfo.name = e.detail.value.name;
        if(!e.detail.value.name) {
            wx.showToast({
                icon: 'none',
                title: '请输入用户名称号',
            })
            return;
        }
        if (this.data.userInfo.mobile) {
            if (!utils.isTelNum(this.data.userInfo.mobile)) {
                wx.showToast({
                    icon: 'none',
                    title: '请输入正确的手机号',
                })
                return;
            }
        }
        network.postJson(api.updateUser, this.data.userInfo).then(res => {
            app.globalData.userInfo = this.data.userInfo;
            notice.postNotice("UserInfo");
            wx.showToast({
                title: '保存成功',
            })
            setTimeout(function () {
                wx.navigateBack();
            }, 2000)
        })
    }
})